package app;

import com.sun.image.codec.jpeg.*;

import java.io.IOException;
import java.io.OutputStream;
import java.awt.*;
import java.awt.image.BufferedImage;

import javax.imageio.IIOImage;
import javax.imageio.ImageTypeSpecifier;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.metadata.IIOMetadata;
import javax.imageio.spi.ImageWriterSpi;
import javax.swing.JComponent;

public class JPGWriter extends ImageWriter {
	public JPGWriter(ImageWriterSpi originatingProvider) {
		super(originatingProvider);
		// TODO Auto-generated constructor stub
	}

	public void write(JComponent myComponent, OutputStream out)
			throws Exception {
		int imgWidth = (int) myComponent.getSize().getWidth(), imgHeight = (int) myComponent
				.getSize().getHeight();
		Dimension size = new Dimension(imgWidth, imgHeight);
		BufferedImage myImage = new BufferedImage(size.width, size.height,
				BufferedImage.TYPE_INT_RGB);
		Graphics2D g2 = myImage.createGraphics();
		myComponent.paint(g2);
		try {
			JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
			encoder.encode(myImage);
			out.close();
		} catch (Exception e) {
			throw new Exception(
					"GRAPHICS   ERROR,CANNOT   CREATE   JPEG   FORMAT");
		}
	}

	@Override
	public IIOMetadata convertImageMetadata(IIOMetadata inData,
			ImageTypeSpecifier imageType, ImageWriteParam param) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public IIOMetadata convertStreamMetadata(IIOMetadata inData,
			ImageWriteParam param) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public IIOMetadata getDefaultImageMetadata(ImageTypeSpecifier imageType,
			ImageWriteParam param) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public IIOMetadata getDefaultStreamMetadata(ImageWriteParam param) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void write(IIOMetadata streamMetadata, IIOImage image,
			ImageWriteParam param) throws IOException {
		// TODO Auto-generated method stub

	}
}
